<template>
	<view class="back">
		<view style="width: 100vw;height: 100vh;background: #fff;margin-top: 30rpx;border-radius: 70rpx 70rpx 0rpx 0rpx;padding-bottom:5vh;">
		<view style="height: 4vh;">
			
		</view>
			<view style="overflow: auto;height: 86vh;width: 100%;">
				<view style="width: 90%;padding: 40rpx;text-align: center;">
					<view>
						添加课程
					</view>
				</view>
				<view style="width: 90%;" >
					<view style="width:90%;margin: 0 auto;height: 180rpx;padding: 5px;box-sizing: border-box;margin-top: 10rpx;" >
						<view style="font-size: 30rpx;float: left;width: 100%;padding: 20rpx">
							<view>
								系列名称：
							</view>
							<view style="margin-top: 20rpx;">
								<u--input
								    placeholder="请输入系列名称"
								    border="surround"
								    v-model="form.seriesName"
								  ></u--input>
							</view>
							<view style="margin-top: 20rpx;">
								课程名称：
							</view>
							<view style="float: left;margin-top: 20rpx;"  @click="drugNameShow = true">
								<input type="text" v-model="seriesName"
									style="width: 120px;height: 30px; background-color: rgb(246, 245, 245);border-radius :20rpx;"
									disabled>
								<u-picker :show="drugNameShow" :columns="drugNameColumns" @confirm="drugNameConfirm" @cancel="closeShow" ref="uPicker"></u-picker>
							</view>
							<view  style="float: left;margin-left: 20rpx;margin-top: 20rpx;" @click="drugNameShows = true">
								<input type="text" v-model="purposeName"
									style="width: 120px;height: 30px; background-color: rgb(246, 245, 245);border-radius :20rpx;"
									disabled>
								<u-picker :show="drugNameShows" :columns="drugNameColumn" @confirm="drugNameConfirms" @cancel="closeShows" ref="uPickers" keyName="label"></u-picker>
							</view>
							<view style="height: 35rpx;float: right;margin-top: 20rpx;">
								<view style="float: right;color:#4089ff;margin-right: 40rpx;" @click="insertPlan">添加</view>
							</view>
							<view style="margin-top: 20rpx;float: left;width: 100%;">
								已添加课程：
							</view>
							<view style="margin-top: 20rpx;float: left;width: 100%; "  v-for="(item, index) in planArray" :key="index">
								<view style="float: left;" @click="xuan(index)">{{item.fitnessPlanName}}</view>
								<view style="float: right;color:red" @click="handleDeletes(index)">删除</view>
								<view style="float: right;margin-right: 15rpx;color:#4089ff" @click.stop="insertAction(index)">添加动作</view>
								<view v-if="item.xuan1">
								<view style="margin-top: 20rpx;float: left;width: 100%;"  v-for="(item, index) in planFrom.planActionArray" >
									<view style="float: left;margin-left: 20rpx;" @click="xuans(index)">{{item.fitnessActionName}}</view>
									<view style="float: right;color:red" @click="handleDeletess(index)">删除</view>
									<view style="float: right;margin-right: 15rpx;color:#4089ff" @click.stop="insertActionCount(index)">添加组数</view>
									<view v-if="item.xuan2">
									<view style="margin-top: 20rpx;float: left;width: 100%;"  v-for="(item, index) in planActionFrom.group">
										<view style="float: left;margin-left: 40rpx;">第{{index+1}}组</view>
										<view style="float: left;margin-left: 10rpx;"><text style="float: left;">每组/</text><input @input="handleInput('actionCount', index)" type="text" v-model="item.actionCount" style="width: 30px;float: left; background-color: rgb(246, 245, 245);border-radius :20rpx;text-align: center;" ><text style="float: left;">次</text></view>
										<view style="float: left;margin-left: 10rpx;"><text style="float: left;">重量：</text><input @input="handleInput('equipmentWeight', index)" type="text" v-model="item.equipmentWeight" style="width: 30px;float: left; background-color: rgb(246, 245, 245);border-radius :20rpx;text-align: center;" ><text style="float: left;">kg</text></view>
										<view style="float: right;color:red" @click="handleDeletesss(index)">删除</view>
									</view>
									</view>
								</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view>
						<u-popup :show="show" @close="close" mode="center" @open="open">
				            <view style="width:600rpx;height:400rpx;padding: 20rpx;">
								<view style="float: left;">
				                <view style="margin-top: 20rpx;width: 600rpx;">
				                	锻炼部位：
				                </view>
				                <view style="float: left;margin-top: 20rpx;"  @click="drugNameShowss = true">
				                	<input type="text" v-model="typeName"
				                		style="width: 120px;height: 30px; background-color: rgb(246, 245, 245);border-radius :20rpx;text-align: center;"
				                		disabled>
				                	<u-picker :show="drugNameShowss" :columns="drugNameColumnss" @confirm="drugNameConfirmss" @cancel="closeShowss" ref="uPickerss" keyName="label"></u-picker>
				                </view>
								</view>
								<view style="float: left;">
								<view style="margin-top: 20rpx;width: 600rpx;">
									训练动作：
								</view>
								<view style="float: left;margin-top: 20rpx;"  @click="drugNameShowsss = true">
									<input type="text" v-model="actionName"
										style="width: 120px;height: 30px; background-color: rgb(246, 245, 245);border-radius :20rpx;text-align: center;"
										disabled>
									<u-picker :show="drugNameShowsss" :columns="drugNameColumnsss" @confirm="drugNameConfirmsss" @cancel="closeShowsss" ref="uPickersss" keyName="label"></u-picker>
								</view>
								<view style="height: 35rpx;float: right;margin-top: 20rpx;">
									<view style="float: right;color:#4089ff;margin-right: 40rpx;" @click="insertPlanAction">添加</view>
								</view>
								</view>
<!-- 								<view style="margin-top: 20rpx;float: left;width: 100%;">
									已添加动作：
								</view>
								<view style="margin-top: 20rpx;float: left;width: 100%; "  v-for="(item, index) in planFrom.planActionArray" :key="index">
									<view style="float: left;">{{item.fitnessActionName}}</view>
									<view style="float: right;color:red" @click="handleDeletess(index)">删除</view>
								</view> -->
								<!-- <view style="margin-top: 20rpx;float: left;width: 100%;">
									已添加课程：
								</view>
								<view style="margin-top: 20rpx;float: left;width: 100%; " v-for="item in planFrom.planActionArray">
									<view style="float: left;">{{item.planName}}</view>
									<view style="float: right;color:red">删除</view>
									<view style="float: right;margin-right: 15rpx;color:#4089ff" @click="insertAction(item.fitnessPurposeId)">添加动作</view>
								</view> -->
				            </view>
						</u-popup>
					</view>
			</view>

			<view style="width: 100%;height: 200rpx;background: #fff;border-radius: 60rpx 60rpx 0rpx 0rpx;padding-bottom:0rpx;box-shadow: 0 2px 10px #dfdfdf;">
				<view style="height:30rpx"></view>
				<view @click="fan" style="margin-left: 100rpx;float: left;width:200rpx;color:#fff;height: 70rpx; border-radius: 10rpx; background-image: linear-gradient(to bottom right, #94A9FD, #9ECFFF, #b9e1ff); display: flex; align-items: center; justify-content: center;">
				  <text style="letter-spacing: 4px;font-size: 24rpx;">&nbsp;返回</text>
				</view>
				<view @click="queding" style="margin-right: 100rpx;float: right;width:200rpx;color:#fff;height: 70rpx; border-radius: 10rpx; background-image: linear-gradient(to bottom right, #94A9FD, #9ECFFF, #b9e1ff); display: flex; align-items: center; justify-content: center;">
				  <text style="letter-spacing: 4px;font-size: 24rpx;">&nbsp;确定</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				xuan1:false,
				xuan2:false,
				show:false,
				purposeId:0,
				typeId:0,
				actionId:0,
				planArray:[],
				planFrom:{
				    fitnessPurposeId:"",
					xuan1: false,
				    planActionArray:[],
				},
				planActionFrom:{
					fitnessActionId:"",
					xuan2: false,
					group:[],
				},
				planActionCountFrom:{
					equipmentWeight:0,
					actionCount:0,
				},
				planActionArrays:[],
				seriesName:"",
				purposeName:"",
				actionName:"",
				typeName:"",
				form:{},
				drugNameColumns:[
					[
					'初级', '中级', '高级', '地狱'
					]
				],
				drugNameColumn:[
					[
						
					]
				],
				drugNameColumnss:[
					[
						
					]
				],
				drugNameColumnsss:[
					[
						
					]
				],
				drugNameShow:false,
				drugNameShowss:false,
				drugNameShowsss:false,
				drugNameShows:false,
				item:{},
				messageText:'',
				title: 'picker',
				errTitle:'',
				selectedIndex:0,
				selectPlan:{},
				selectId:0,
				multiArray: [
				],
				index: 0,
				show: false,
				errShow:false,
				courseList: [
				],
				planList:[],
				planLists:[],
					current: 0,
					selectStatus:1,
					dayOfMonth: 0,
					daySelect: 0,
					today: '',
					msgType: 'sussess'
			}
		},
		onLoad(option) {
			// this.item = JSON.parse(option.item) //打印出上个页面传递的参数。
			this.getReservation()
			this.queryFitnessPurpose()
			// this.getPlan()
			this.getAction()
		},
		methods: {
			 handleInput(type, index) {
			        // 这里使用正则表达式检查输入是否为数字
			        const regex = /^\d*$/;
			
			        if (type === 'actionCount' && !regex.test(this.planActionFrom.group[index].actionCount)) {
			            // 如果不是数字，可以进行相应的处理，比如清除非数字字符
			            this.planActionFrom.group[index].actionCount = this.planActionFrom.group[index].actionCount.replace(/\D/g, '');
			        }
			
			        if (type === 'equipmentWeight' && !regex.test(this.planActionFrom.group[index].equipmentWeight)) {
			            // 如果不是数字，可以进行相应的处理，比如清除非数字字符
			            this.planActionFrom.group[index].equipmentWeight = this.planActionFrom.group[index].equipmentWeight.replace(/\D/g, '');
			        }
			    },
			xuan(index){
				console.log(this.planArray)
				uni.setStorageSync('index',index)
				this.planFrom.planActionArray=this.planArray[index].planActionArray
				console.log(111)
				this.planArray.forEach((item, i) => {
				    if (i === index) {
				      this.$set(this.planArray, index, {				            ...this.planArray[index],				            xuan1: !this.planArray[index].xuan1				        });
				    } else {
				      item.xuan1 = false; // Set others to false
				    }
				  });
			},
			xuans(index){
				this.planFrom.planActionArray=this.planArray[uni.getStorageSync('index')].planActionArray
				this.planArray[uni.getStorageSync('index')].xuan1=true
				this.planActionFrom.group=this.planFrom.planActionArray[index].group
				this.planFrom.planActionArray.forEach((item, i) => {
				    if (i === index) {
				      this.$set(this.planFrom.planActionArray, index, {
				            ...this.planFrom.planActionArray[index],
				            xuan2: !this.planFrom.planActionArray[index].xuan2
				        });
				    } else {
				      item.xuan2 = false; // Set others to false
				    }
				  });
				console.log(222)
				// console.log(this.planFrom.planActionArray)
			},
			handleDeletes(index){
			        this.planArray.splice(index, 1);
			      },
			handleDeletess(index){
				    this.planFrom.planActionArray.splice(index, 1);
				  },
			handleDeletesss(index){
				  	this.planActionFrom.group.splice(index, 1);
				  },
			queding(){
				
				this.form.planArray=this.planArray
				this.form.coachId=uni.getStorageSync('teacherinfo').id
				this.form.venueUniqueIdentifier=uni.getStorageSync('teacherinfo').venueUniqueIdentifier
				console.log(this.form)
				uni.request({
					url: this.URL+'plan/addFitnessSeries',
					method: 'post',
					data:this.form,
					// userInfo:res.
					success: function(res) {
						uni.navigateBack({
						        delta: 1 // 返回的页面数，这里是返回上一页，所以是1
						    });
					},
					fail: function(error) {
						// 处理请求失败
						console.error('查询失败,请检查网络');
					},
					
				})
			},
			open() {
			},
			close() {
				this.typeName=""
				this.actionName=""
				this.drugNameColumnss[0]=[]
				this.drugNameColumnsss[0]=[]
				this.planActionFrom={}
				            this.planFrom={
				                fitnessPurposeId:"",
				                planActionArray:[],
				
				            }
				this.show=false
			},
			insertAction(index){
				console.log(index)
				this.planFrom.planActionArray=this.planArray[index].planActionArray
				console.log(55555+JSON.stringify(this.planFrom.planActionArray))
				var fitnessPurposeId=this.planArray[index].fitnessPurposeId
				console.log( 444444+fitnessPurposeId)
				var ths = this;
				uni.request({
					url: this.URL+'plan/getFitnessTypes',
					method: 'GET',
					data:{
						fitnessPurposeId:fitnessPurposeId,
					},
					// userInfo:res.
					success: function(res) {
						for (const item of res.data.data.content) {
							ths.drugNameColumnss[0].push({
								id:item.id,
								label:item.fitnessTypeName,
							})
						}
					},
					fail: function(error) {
						// 处理请求失败
						console.error('查询失败,请检查网络');
					}
				})
				
				this.show=true
				
			},
			insertPlan(){
				this.planFrom={
				            fitnessPurposeId:"",
				            planActionArray:[],
				            }
				this.planFrom.fitnessPlanName=this.seriesName+"练"+this.purposeName+"系列"
				this.planFrom.venueUniqueIdentifier=uni.getStorageSync('teacherinfo').venueUniqueIdentifier,
				this.planFrom.coachId=uni.getStorageSync('teacherinfo').id
				this.planFrom.xuan1=false
				this.planFrom.fitnessPurposeId=this.purposeId
				this.planArray.push(this.planFrom)
				console.log(this.planArray)
				this.seriesName=""
				this.purposeName=""
				
			},
			insertPlanAction(){
				this.planActionFrom={
					fitnessActionId:"",
					xuan2: false,
					group:[],
				}
				this.planActionFrom.fitnessActionName=this.actionName
				this.planActionFrom.fitnessPlanId=0
				this.planActionFrom.fitnessActionId=this.actionId
				this.planActionFrom.xuan2=false
				this.planActionFrom.venueUniqueIdentifier=uni.getStorageSync('teacherinfo').venueUniqueIdentifier,
				this.planFrom.planActionArray.push(this.planActionFrom)
				var ths = this;
				uni.request({
					url: this.URL+'plan/getFitnessPlanActionGroupss',
					method: 'GET',
					data:{
						planActionId:this.actionId
					},
					// userInfo:res.
					success: function(res) {
						ths.planActionFrom.group=res.data.data
						console.log(ths.planActionFrom.group)
						ths.typeName=""
						ths.actionName=""
						ths.drugNameColumnss[0]=[]
						ths.drugNameColumnsss[0]=[]
						ths.planActionFrom={}
						            ths.planFrom={
						                fitnessPurposeId:"",
						                planActionArray:[],
						
						            }
						ths.show=false
					},
					fail: function(error) {
						// 处理请求失败
						console.error('查询失败,请检查网络');
					}
				})
				console.log(this.planFrom.planActionArray)
			},
			insertActionCount(index){
				this.planActionFrom.group=this.planFrom.planActionArray[index].group
				console.log(this.planActionFrom.group)
				this.planActionCountFrom.venueUniqueIdentifier=uni.getStorageSync('teacherinfo').venueUniqueIdentifier,
				this.planActionFrom.group.push(this.planActionCountFrom)
			},
			queryFitnessPurpose(){
				var ths = this;
				uni.request({
					url: this.URL+'plan/getFitnessPurpose',
					method: 'GET',
					data:{
						venueUniqueIdentifier:uni.getStorageSync('teacherinfo').venueUniqueIdentifier,
					},
					// userInfo:res.
					success: function(res) {
						for (const item of res.data.data.content) {
							ths.drugNameColumn[0].push({
								id:item.id,
								label:item.fitnessPurposeName,
							})
						}
					},
					fail: function(error) {
						// 处理请求失败
						console.error('查询失败,请检查网络');
					}
				})
			},
			closeShow() { //控制取消病种和生命周期的下拉菜单
				this.drugNameShow = false
			},
			closeShows() { //控制取消病种和生命周期的下拉菜单
				
				this.drugNameShows = false
			},
			closeShowss() { //控制取消病种和生命周期的下拉菜单
				this.drugNameShowss = false
			},
			closeShowsss() { //控制取消病种和生命周期的下拉菜单
				this.drugNameShowsss = false
			},
			drugNameConfirm(e) { //病种的下拉菜单
				e.value.forEach((item, index) => {
					if (index >= 0) {
						this.seriesName = item
					} else {
						this.patientForm.drugName = ' '
					}
				})
				this.drugNameShow = false
			},
			drugNameConfirms(e) { //病种的下拉菜单
				e.value.forEach((item, index) => {
					if (index >= 0) {
						this.purposeName = item.label
						this.purposeId=item.id
					} else {
						this.patientForm.drugName = ' '
					}
				})
				this.drugNameShows = false
			},
			drugNameConfirmsss(e) { //病种的下拉菜单
				e.value.forEach((item, index) => {
					if (index >= 0) {
						this.actionName = item.label
						this.actionId=item.id
					} else {
						this.patientForm.drugName = ' '
					}
				})
				this.drugNameShowsss = false
			},
			drugNameConfirmss(e) { //病种的下拉菜单
				e.value.forEach((item, index) => {
					if (index >= 0) {
						this.typeName = item.label
						this.typeId=item.id
					} else {
						this.patientForm.drugName = ' '
					}
				})
				var ths = this;
				uni.request({
					url: this.URL+'plan/queryByType',
					method: 'GET',
					data:{
						venueUniqueIdentifier:uni.getStorageSync('teacherinfo').venueUniqueIdentifier,
						fitnessTypeId:this.typeId,
						fitnessPurposeId:this.purposeId,
					},
					// userInfo:res.
					success: function(res) {
						for (const item of res.data.data.data) {
							ths.drugNameColumnsss[0].push({
								id:item.id,
								label:item.fitnessActionName,
							})
						}
					},
					fail: function(error) {
						// 处理请求失败
						console.error('查询失败,请检查网络');
					}
				})
				this.drugNameShowss = false
			},
			fan(){
				uni.navigateBack({
				        delta: 1 // 返回的页面数，这里是返回上一页，所以是1
				    });
			},
			getAction() {
				
			},
			selectUser(item){
				this.selectId=item.id
			},
			handleChange(event) {
				const selectedIndex = event.detail.value; // 获取选中的索引
				const selectPlan = this.planList[selectedIndex]; // 获取选中的对象数据
				var ths = this;
				uni.request({
					url: this.URL+'vipPlan/memberAddPlan',
					method: 'POST',
					data: {
						memberId: ths.selectId,
						seriesId: selectPlan.id
					},
					// userInfo:res.
					success: function(res) {
						if(res.data.code==200){
							this.getReservation()
							ths.show=true
							setTimeout(() => {
							        ths.show = false;
							      }, 2000);
						}else{
							ths.errShow=true
							ths.errTitle=res.data.data
							setTimeout(() => {
							        ths.errShow = false;
							      }, 2000);
						}
						
					},
					fail: function(error) {
						// 处理请求失败
						console.error('角色查询失败,请检查网络');
					}
				})
				
			},
			getReservation(){
				var ths = this;
				uni.request({
					url: this.URL+'AppCoach/getStudentByTeacherId',
					method: 'GET',
					data: {
						coachCode: uni.getStorageSync('teacherinfo').coachCode,
					},
					// userInfo:res.
					success: function(res) {
						ths.courseList = res.data.data;
					},
					fail: function(error) {
						// 处理请求失败
						console.error('角色查询失败,请检查网络');
					}
				})
			},
			getPlan(){
				var ths = this;
				uni.request({
					url: this.URL+'plan/getFitnessPlanActionCount',
					method: 'GET',
					data: {
						planActionId: this.item.id,
					},
					// userInfo:res.
					success: function(res) {
						ths.planList = res.data.data.content;
						console.log(ths.planList)
					},
					fail: function(error) {
						// 处理请求失败
						console.error('查询失败,请检查网络');
					}
				})
			},
			goSearch() {	
				
			},
			openTrain(item){
				uni.navigateTo({
					url: '/pages/Reservation/Reservation?item='+JSON.stringify(item)
				})
			},
		}
	}
</script>

<style>
	.title {
			font-size: 14px;
			font-weight: bold;
			margin: 20px 0 5px 0;
		}
	.data-pickerview {
			height: 400px;
			border: 1px #e5e5e5 solid;
		}
	
		 .popper__arrow {
	    top: -6px;
	    left: 50%;
	    margin-right: 3px;
	    border-top-width: 0;
	    border-bottom-color: #EBEEF5;
	}
	 .popper__arrow {
	    top: -6px;
	    left: 50%;
	    margin-right: 3px;
	    border-top-width: 0;
	    border-bottom-color: #EBEEF5;
	}
	.week-item-d1{
		background-color: #9da4ff;
		width: 23px;height:23px;
		margin: 0 auto;
		border:#92A3FD;
		border-radius: 50%;
		margin-top: 1vh;
		font-size: 28rpx;
		padding-top: 2px;
		box-sizing: border-box;
		padding-right: 3rpx;
	}
	.div2{
		width: 100%;
		height: 5vh;
	}
	.div2-2{
		background-color:#fff;
		border-radius: 20px 20px;
		height: 70rpx;
		width: 90%;
		margin: 0 auto;
	}
/* ::v-deep .div2-2 .input-placeholder {
    color: #FFF;
} */
.week-display {
	display: flex;
	justify-content: space-between;
	color: #fff;
	width: 90%;
	height: 8vh;
	margin: 0 auto;
	margin-top: 2vh;
}

.week-item {
	flex: 1;
	text-align: center;
}

.back {
	position: relative;
	width: 100%;
	height: 100vh;
	overflow: hidden; /* 超出部分隐藏，可根据需求调整 */
}
.homeclick{
	height: 70rpx;
	width: 70rpx;
	margin-top: 20rpx;
}
.dao{
	 float: left;
	 width: 50%;
	 margin: 0 auto;
	 margin-top: 20rpx;
	 height: 45rpx;
	 font-size: 25rpx;
}
.dao-1{
	 border-bottom: skyblue 3px solid;
	 width: 30%;
	 margin: 0 auto;
	 height: 45rpx;
	 display: flex;
	 justify-content: center;
	 align-items: center;
}
.dao-2{
	 width: 30%;
	 margin: 0 auto;
	 height: 45rpx;
	 display: flex;
	 justify-content: center;
	 align-items: center;
}
.imagg{
	 width: 140rpx;
	 height: 140rpx;
	 border-radius: 4px;
}
</style>
